<template>
  <div class="setting -container">
    <div class="app-container">
      <el-card v-loading="isLoading">
        <!-- 顶部 tab栏 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <!-- 新增角色按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="openDialog"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="100px" :index="indexMethod" />
              <el-table-column label="角色名称" prop="name" width="150px" />
              <el-table-column label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button type="success" size="small">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4]"
              :page-size="pagesize"
              layout="total,sizes, prev, pager, next,jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息" name="info">
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称"><el-input v-model="companyForm.name" disabled style="width:400px" /></el-form-item>
              <el-form-item label="公司地址"><el-input v-model="companyForm.companyAddress" disabled style="width:400px" /></el-form-item>
              <el-form-item label="邮箱"><el-input v-model="companyForm.mailbox" disabled style="width:400px" /></el-form-item>
              <el-form-item label="备注"><el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" /></el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

      </el-card>
      <!-- 新增角色弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>
        <!-- 按钮 -->
        <template #footer>
          <el-button>取消</el-button>
          <el-button type="primary" @click.native="clickSubmit">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqAllRoleList, reqDelRole, reqAddRole, reqGetRoleDetail, reqUpdateRole, reqGetCompanyById } from '@/api/setting'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'role',
      page: 1,
      pagesize: 2,
      total: 0,
      list: [],
      isLoading: false,
      showDialog: false,
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur,change'] },
          { min: 1, max: 10, message: '输入1-10个字符', trigger: ['change', 'blur'] }
        ],
        description: [
          { required: true, message: '请进行角色描述', trigger: ['blur', 'change'] },
          { min: 1, max: 200, message: '不能超过200个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  // ** 编辑和删除共用弹框 ，动态设置标题
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    ...mapState('user', ['userInfo']),
    async getRoleList() {
      this.isLoading = true
      const { data: { rows, total }} = await reqAllRoleList(this.page, this.pagesize)
      this.list = rows
      this.total = total
      this.isLoading = false
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getRoleList()
    },
    // ** 分页 **
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    // ** 删除 **
    delRole(id) {
      this.$confirm('您确定删除吗', '提示').then(async() => {
        await reqDelRole(id)
        this.$message.success('删除成功')
        this.getRoleList()
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
      }).catch(() => {
        this.$message.error('删除失败')
      })
    },
    openDialog() {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields() // 关闭弹框的时候重置
      this.form = {
        name: '',
        description: ''
      }
    },
    clickSubmit() {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        // 判断新增/添加
        if (this.form.id) {
          // 编辑
          await reqUpdateRole(this.form)
          this.$message.success('编辑成功')
        } else {
          // 新增
          await reqAddRole(this.form)
          this.$message.success('新增角色成功')
        }
        this.showDialog = false
        this.getRoleList()
      })
    },
    // ** 编辑 **
    async editRole(id) {
      const { data } = await reqGetRoleDetail(id)
      this.form = data
      this.showDialog = true
    },
    // ** 公司信息 **
    async getCompanyInfo() {
      const { data } = await reqGetCompanyById(this.userInfo.companyId)
      this.companyForm = data
    }

  }

}
</script>

<style>

</style>
